/**
 * FollowApi.java
 * com.zhihu.follow.api
 * Copyright (c) 2016, 北京聚智未来科技有限公司版权所有.
*/

package com.zhihu.follow.api;

import java.util.Date;

import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.DbPro;
import com.jfinal.plugin.activerecord.Record;
import com.zhihu.follow.model.Follow;

/**
 * 关注
 * @author   wxl
 * @Date	 2016年12月26日 	 
 */
public class FollowApi extends Controller {

	/**
	 * 先查一下是否已关注
	 * 关注问题
	 * @param 问题id
	 */
	public void followProblem() {
		String pid = getPara(0);
		String userid = getPara(1);

		Follow follow = Follow.dao.findFollow(pid, userid);
		if (follow == null) {
			Date date = new Date();
			Record record = new Record().set("oid", pid).set("userid", userid).set("createdate", date).set("isuser", 0);
			DbPro.use().save("z_follow", "oid,userid", record);
			setAttr("ds", record);
		} else {
			int deleteFollow = Follow.dao.deleteFollow(pid, userid);
			setAttr("ds", deleteFollow);
		}
		render("/eova/widget/form/gameTask.html");
		renderJson();
	}

	/**
	 * 关注人 
	 * @param 人pid
	 */
	public void followPerson() {
		String pid = getPara(0);
		String userid = getPara(1);
		Follow follow = Follow.dao.findFollow(pid, userid);
		if (follow == null) {
			Date date = new Date();
			Record record = new Record().set("oid", pid).set("userid", userid).set("createdate", date).set("isuser", 1);
			DbPro.use().save("z_follow", "oid,userid", record);
			setAttr("ds", record);
		} else {
			int deleteFollow = Follow.dao.deleteFollow(pid, userid);
			setAttr("ds", deleteFollow);
		}
		render("/eova/widget/form/gameTask.html");
		renderJson();
	}
}
